package backend

import (
	"blog/service/page"
	"net/http"
	"strconv"

	"gitee.com/xchengli/blog-lib/session"

	"github.com/gin-gonic/gin"
)

/*
*
 */
func AdminTypeHandle(ctx *gin.Context) {
	guest := new(session.GuestSession)
	//检查是否未登录
	if guest.Init(ctx.Request).CheckIsLogin() == false {
		ctx.JSON(http.StatusServiceUnavailable, gin.H{"error": "未登录"})
		return
	}

	ctx.HTML(http.StatusOK, "adminType.html", nil)
}

/**
 * @api            		{GET} /admin/types/all 获取所有的分类
 * @apiName        		admin types all
 * @apiGroup       		admin/types
 * @apiVersion     		1.0.0
 * @apiDescription 		获取所有的分类
 * @apiPermission  		admin
 * @apiSampleRequest 	/admin/types/all
 * @apiParam {number} 	fid           	父级id
 */
func AdminTypeAllHandle(ctx *gin.Context) {
	guest := new(session.GuestSession)
	//检查是否未登录
	if guest.Init(ctx.Request).CheckIsLogin() == false {
		ctx.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
		return
	}

	// 使用PS层服务
	adminTypeService := page.NewAdminTypePageService()
	types := adminTypeService.GetAllTypes(ctx)
	ctx.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": types})
}

/**
 * @api            		{POST} /admin/types/add 添加分类
 * @apiName        		admin types add
 * @apiGroup       		admin/types
 * @apiVersion     		1.0.0
 * @apiDescription 		添加分类
 * @apiPermission  		admin
 * @apiSampleRequest 	/admin/types/add
 * @apiParam {number} 	fid           	父级id
 * @apiParam {string} 	name          	名称
 * @apiParam {number} 	read_level    	阅读账号登陆
 * @apiParam {number} 	is_show       	是否显示 1是0否
 */
func AdminTypeAddHandle(ctx *gin.Context) {
	guest := new(session.GuestSession)
	//检查是否未登录
	if guest.Init(ctx.Request).CheckIsLogin() == false {
		ctx.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
		return
	}

	// 使用PS层服务
	adminTypeService := page.NewAdminTypePageService()

	// 使用 ps 层创建数据结构
	postType := adminTypeService.CreateTypeName()

	//fid 父级ID
	fid, _ := strconv.ParseInt(ctx.PostForm("fid"), 10, 64)
	is_fid_ok, errMsg := adminTypeService.ValidateTypeFID(ctx, postType, int(fid))
	if is_fid_ok == false {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": errMsg})
		return
	}
	postType.FID = int(fid)

	//name 名称
	name := ctx.PostForm("name")
	is_name_ok, errMsg := adminTypeService.ValidateTypeName(ctx, postType, name, postType.FID)
	if is_name_ok == false {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": errMsg})
		return
	}
	postType.Name = name

	//read_level
	read_level, _ := strconv.ParseUint(ctx.PostForm("read_level"), 10, 8)
	postType.ReadLevel = int(read_level)

	//is_show
	is_show, _ := strconv.ParseUint(ctx.PostForm("is_show"), 10, 8)
	if is_show == 0 {
		postType.IsShow = 0
	} else {
		postType.IsShow = 1
	}

	postType.FName = adminTypeService.GetTypeName(ctx, postType.FID)

	err := adminTypeService.CreateType(ctx, postType)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "添加失败", "data": postType})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": postType})
}

/**
 * @api            		{POST} /admin/types/update 修改分类
 * @apiName        		admin types update
 * @apiGroup       		admin/types
 * @apiVersion     		1.0.0
 * @apiDescription 		添加分类
 * @apiPermission  		admin
 * @apiSampleRequest 	/admin/types/update
 * @apiParam {number} 	id           	id
 * @apiParam {number} 	fid           	父级id
 * @apiParam {string} 	name          	名称
 * @apiParam {number} 	read_level    	阅读账号登陆
 * @apiParam {number} 	is_show       	是否显示 1是0否
 */
func AdminTypeUpdateHandle(ctx *gin.Context) {
	guest := new(session.GuestSession)
	//检查是否未登录
	if guest.Init(ctx.Request).CheckIsLogin() == false {
		ctx.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
		return
	}

	// 使用PS层服务
	adminTypeService := page.NewAdminTypePageService()

	id, _ := strconv.ParseInt(ctx.PostForm("id"), 10, 64)
	postType, err := adminTypeService.GetTypeByID(ctx, id)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": "分类未找到"})
		return
	}

	//fid 父级ID
	fid, _ := strconv.ParseInt(ctx.PostForm("fid"), 10, 64)
	is_fid_ok, errMsg := adminTypeService.ValidateTypeFID(ctx, postType, int(fid))
	if is_fid_ok == false {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": errMsg})
		return
	}
	postType.FID = int(fid)

	//name 名称
	name := ctx.PostForm("name")
	is_name_ok, errMsg := adminTypeService.ValidateTypeName(ctx, postType, name, postType.FID)
	if is_name_ok == false {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": errMsg})
		return
	}
	postType.Name = name

	//read_level
	read_level, _ := strconv.ParseUint(ctx.PostForm("read_level"), 10, 8)
	postType.ReadLevel = int(read_level)

	//is_show
	is_show, _ := strconv.ParseUint(ctx.PostForm("is_show"), 10, 8)
	if is_show == 0 {
		postType.IsShow = 0
	} else {
		postType.IsShow = 1
	}

	postType.FName = adminTypeService.GetTypeName(ctx, postType.FID)

	err = adminTypeService.UpdateType(ctx, postType)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "更新失败"})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": postType})
}
